﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using MyToDoList.Model;
using MyToDoList.ViewModel;

namespace MyToDoList
{
    public partial class DetailElement : PhoneApplicationPage
    {

        private Task _task;
        private bool _isNew;

        public DetailElement()
        {
            InitializeComponent();
            _task = new Task();
            _isNew = true;
        }

        private void btSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!allFieldsValued())
                {
                    MessageBox.Show("Some fields are not valued", "Warning", MessageBoxButton.OK);
                    return;
                }
                    
                _task.Done = false;
                _task.Description = txtDescription.Text;
                _task.Title = txtTitle.Text;

                //App.ViewModel.Items.Add(task);
                if (_isNew)
                    App.ViewModel.Items.Add(_task);
                else
                    App.ViewModel.Update(_task);
                App.CurrentObject = null;
                App.ViewModel.IsDataLoaded = false;
                //App.ViewModel.StoreData();
                this.NavigationService.GoBack();//.Navigate(new Uri("/MyToDoList;component/UI/MainPage.xaml", UriKind.RelativeOrAbsolute));
            }
            catch(Exception ex)
            {

            }
            
        }

        

        private bool allFieldsValued()
        {
            bool valued = false;
            if (!string.IsNullOrEmpty(txtDescription.Text) || !string.IsNullOrWhiteSpace(txtDescription.Text))
                if (!string.IsNullOrEmpty(txtTitle.Text) || !string.IsNullOrWhiteSpace(txtTitle.Text))
                    valued = true;
            return valued;
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string title = "";
            string description = "";
            string n = "";

            if (App.CurrentObject != null)
            {
                _task = App.CurrentObject;
                /*if (NavigationContext.QueryString.TryGetValue("title", out title))*/
                    txtTitle.Text = _task.Title;

                //if (NavigationContext.QueryString.TryGetValue("description", out description))
                    txtDescription.Text = _task.Description;

                _isNew = false;
            }

        }
    }
}